package com.chdwmp.product.pay.wx;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class ResponseUtil {

    /**
     * 发送文本。使用UTF-8编码。
     * @param response
     * @param text 发送的字符串
     */
    public static void renderText(HttpServletResponse response, String text) throws Exception{
        render(response, "text/plain;charset=UTF-8", text);
    }

    /**
     * 发送json。使用UTF-8编码。
     * @param response
     * @param text 发送的JSON字符串
     */
    public static void renderJson(HttpServletResponse response, String text) throws Exception{
        render(response, "application/json;charset=UTF-8", text);
    }

    /**
     * 发送xml 使用UTF-8编码。
     * @param response
     * @param text 发送的字符串
     */
    public static void renderXml(HttpServletResponse response, String text) throws Exception{
        render(response, "text/xml;charset=UTF-8", text);
    }

    /**
     * 发送内容。使用UTF-8编码。
     * @param response
     * @param contentType
     * @param text
     */
    public static void render(HttpServletResponse response, String contentType, String text) throws Exception{
        response.setContentType(contentType);
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        PrintWriter pWriter = response.getWriter();
        pWriter.write(text);
        pWriter.flush();
        pWriter.close();
    }
}
